#include "StdAfx.h"
#include <string>
#include <Util/StringUtil.h>
#include <interface/data_op/table_wrapper.h>
#include "whitename_op.h"

namespace aot{ namespace data{


    WhiteNameOp::WhiteNameOp(void)
    {

    }

    WhiteNameOp::~WhiteNameOp(void)
    {

    }

    bool WhiteNameOp::insert(const char* szBuddyNum)
    {
        if (!Global_Db) return false;

        if ( this->find(szBuddyNum) )
        {
            return false;
        }

        std::string strBuddyNum = szBuddyNum;
        RemoveSinQuot(strBuddyNum);

        std::string strSqlFmt = 

            "INSERT INTO FileWhiteNums \
            (BuddyNum) \
            VALUES\
            ('%s');";



        std::string strSql;
        

        strSql = aot::tt::utility::StrUtil::Printf(strSqlFmt.c_str(),
            strBuddyNum.c_str()
            );

        return Global_Db->exec(strSql.c_str());
    }

    bool WhiteNameOp::del(const char* szBuddyNum)
    {
        std::string strBuddyNum = szBuddyNum;
        RemoveSinQuot(strBuddyNum);

        std::string strSqlFmt;
        std::string strSql;
        
        strSqlFmt = "delete from FileWhiteNums where BuddyNum='%s'";
        strSql = aot::tt::utility::StrUtil::Printf(strSqlFmt.c_str(), strBuddyNum.c_str());

        return Global_Db->exec(strSql.c_str());
    }

    bool WhiteNameOp::find(const char* szBuddyNum)
    {
        std::string strBuddyNum = szBuddyNum;
        RemoveSinQuot(strBuddyNum);
        int nTotal;
        std::string strSqlFmt = "select count(*) as T from FileWhiteNums where BuddyNum='%s';";
        std::string strSql = aot::tt::utility::StrUtil::Printf(strSqlFmt.c_str(), strBuddyNum.c_str());
        nTotal = Global_Db->exec_scalar(strSql.c_str());
        return (nTotal>0);
    }

    bool WhiteNameOp::get_whitenames(aot::tt::ivec_str** NamesList)
    {
        aot::tt::vec_str_impl* p = new aot::tt::vec_str_impl;
        *NamesList = p;

        std::string strSql;
        strSql = "select BuddyNum from FileWhiteNums;";
        aot::data::table_helper _table;

        bool bRet = Global_Db->exec_and_get_record(strSql.c_str(), _table);

        std::string strValue;
        for(int i=0; i<_table.get_rows_count(); i++)
        {
            _table.set_row_index(i);

            strValue = _table.get_field_value_str("BuddyNum");
            p->impl().push_back(strValue);
        }

        return bRet;
    }

    bool WhiteNameOp::update_whitenames(aot::tt::ivec_str* NewNamesList)
    {
        if (NewNamesList->size() == 0)
        {
            return false;
        }

        this->begin_transaction();

        Global_Db->exec("delete from FileWhiteNums;");

        for(NewNamesList->goto_begin(); !NewNamesList->is_end(); NewNamesList->goto_next())
        {
            this->insert(NewNamesList->get_value());
        }

        return this->commit_transaction();
    }


}} /* end namespace aot/data*/
